package com.jandan.persistence.sqlmapdao;

import java.sql.SQLException;
import java.util.List;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;
import com.jandan.persistence.iface.BlogArticleDao;
import com.jandan.ui.model.BlogArticle;

/**
 * BlogArticleDao接口实现类，使用Ibatis框架进行持久化
 * @author Gong Yong
 * @version 1.0
 * @see com.jandan.persistence.iface.BlogArticleDao
 */
public class BlogArticleSqlMapDao extends SqlMapClientDaoSupport implements BlogArticleDao {

	public List<BlogArticle> getAllBlogArticleList() {
		// TODO Auto-generated method stub
		return this.getSqlMapClientTemplate().queryForList("BlogArticle.getAllBlogArticleList");
	}
	
	public List<BlogArticle> getAllBlogArticleList(int start, int limit) {
		// TODO Auto-generated method stub
		return this.getSqlMapClientTemplate().queryForList("BlogArticle.getAllBlogArticleList",start,limit);
	}

	public BlogArticle getBlogArticleByArticleID(long articleID) {
		// TODO Auto-generated method stub
		return (BlogArticle)this.getSqlMapClientTemplate().queryForObject("BlogArticle.getBlogArticleByArticleID", articleID);
	}

	public long insertBlogArticle(BlogArticle article) {
		// TODO Auto-generated method stub
		long articleID = 0;
		try {
			this.getSqlMapClient().startTransaction();
			this.getSqlMapClientTemplate().insert("BlogArticle.insertBlogArticle",article);
			articleID = (Long)this.getSqlMapClientTemplate().queryForObject("BlogArticle.lastInsertBlogArticleID");
			this.getSqlMapClient().commitTransaction();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				this.getSqlMapClient().endTransaction();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return articleID;
	}

	public void updateBlogArticle(BlogArticle article) {
		// TODO Auto-generated method stub
		this.getSqlMapClientTemplate().update("BlogArticle.updateBlogArticle",article);
	}

	public void deleteBlogArticle(long articleID) {
		// TODO Auto-generated method stub
		this.getSqlMapClientTemplate().delete("BlogArticle.deleteBlogArticle",articleID);
	}

	public int getTotalBlogArticleCount() {
		// TODO Auto-generated method stub
		return (Integer)getSqlMapClientTemplate().queryForObject("BlogArticle.getTotalArticleCount");
	}

	public List<BlogArticle> getAllBlogArticleListByCategory(int start,
			int limit, String category) {
		// TODO Auto-generated method stub
		return this.getSqlMapClientTemplate().queryForList("BlogArticle.getAllBlogArticleListByCategory",category,start,limit);
	}

	public int getTotalBlogArticleCountByCategory(String category) {
		// TODO Auto-generated method stub
		return (Integer)getSqlMapClientTemplate().queryForObject("BlogArticle.getTotalArticleCountByCategory",category);
	}
}
